package file

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"mime/multipart"
	"path"
)

type FileBean struct {
	Id             int    `gorm:"column:id;primary_key;AUTO_INCREMENT" form:"id" json:"id"`
	Identification string `gorm:"column:identification" form:"identification" json:"identification"` //标识
	Module         string `gorm:"column:module" form:"module" json:"module"`                         //分类
	Type           string `gorm:"column:type" form:"type" json:"type"`                               //分类
	Path           string `gorm:"column:path" form:"path" json:"path,omitempty"`                     //路径
	Name           string `gorm:"column:name" form:"name" json:"name,omitempty"`                     //文件名
	Size           int64  `gorm:"column:size" form:"size" json:"size,omitempty"`                     //文件大小
	Suffix         string `gorm:"column:suffix" form:"suffix" json:"suffix,omitempty"`               //文件后缀
	CreateTime     string `gorm:"column:create_time" form:"createTime" json:"createTime,omitempty"`  //文件后缀
}

func (fileBean *FileBean) FileSave(c *gin.Context, db *gorm.DB, file *multipart.FileHeader) (*gorm.DB, *FileBean) {
	err := c.SaveUploadedFile(file, fileBean.Path)
	if err != nil {
		db.Error = err
		return db, nil
	}
	fileBean.Size = file.Size                 //文件大小
	fileBean.Suffix = path.Ext(file.Filename) //文件后缀
	fileBean.Path = fileBean.Path[1:]
	db.Table("file_info").Create(&fileBean)
	return db, fileBean
}
